package com.gis3c.sys.dao.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.gis3c.sys.entry.po.Role;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.gis3c.sys.dao.RoleDao;

@Repository
public class RoleDaoImpl extends SqlSessionDaoSupport implements RoleDao {
	@Resource(name="sqlSessionFactory_sys") private SqlSessionFactory sqlSessionFactory;
	
	@Override
	public Role CreateRole(Role role) {
		getSqlSession().insert("RoleSql.createRole",role);
		return role;
	}

	@Override
	public void DeleteRole(String roleId) {
		getSqlSession().delete("RoleSql.deleteRole",roleId);
	}
	
	@Override
	public void DeleteRoles(String[] roleIds){
		getSqlSession().delete("RoleSql.deleteRoles",Arrays.asList(roleIds));
	}
	
	@Override
	public void UpdateRole(Role newRole){
		getSqlSession().update("RoleSql.updateRole",newRole);
	}
	
	@Override
	public Role FindRoleById(String roleId){
		return getSqlSession().selectOne("RoleSql.findRoleById",roleId);
	}
	
	@Override
	public List<Role> FindRoleByName(String roleName){
		return getSqlSession().selectList("RoleSql.findRolesByName",roleName);
	}
	
	@Override
	public List<Role> FindRoleByAliasName(String aliasName){
		return getSqlSession().selectList("RoleSql.findRolesByAliasname",aliasName);
	}

	@Override
	public List<Role> findRolesByUserName(String userName) {
		return getSqlSession().selectList("RoleSql.findRolesByUserName",userName);
	}

	@Override
	public List<Role> findRolesByUserId(String usernId) {
		return getSqlSession().selectList("RoleSql.findRolesByUserId",usernId);
	}

	@Override
	public List<Role> RoleList(Integer pageSize,Integer currentPage) {
		Map<String,Integer> params = new HashMap<>();
		params.put("pageSize",pageSize);
		params.put("currentPage",currentPage);
		return getSqlSession().selectList("RoleSql.roleList",params);
	}

	@Override
	public Integer RoleCount() {
		return getSqlSession().selectOne("RoleSql.RoleCount");
	}

    @Override
    public void correlationRoles(String userId, String... roleIds) {
        Map<String,Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("roleIds", roleIds);

        getSqlSession().insert("RoleSql.correlationRoles", params);
    }

    @Override
    public void uncorrelationRoles(String userId,String... roleIds){
    	Map<String,Object> params = new HashMap<>();
		params.put("userId",userId);
		params.put("roleIds",Arrays.asList(roleIds));
        getSqlSession().delete("RoleSql.uncorrelationRoles",params);
    }

    @Override
	public Integer correlationMenus(String roleId, String appId, String... menuIds){
    	Map<String,Object> params = new HashMap<>();
		params.put("roleId",roleId);
		params.put("appId",appId);
		params.put("menuIds",menuIds);
		return getSqlSession().insert("RoleSql.correlationMenus",params);
	}
	
	@Override
	public Integer uncorrelationMenus(String roleId) {
		return getSqlSession().insert("RoleSql.uncorrelationMenus",roleId);
	}

	@Override
	public Integer uncorrelationMenus(String roleId, String appId) {
		Map<String,Object> params = new HashMap<>();
		params.put("roleId",roleId);
		params.put("appId",appId);
		return getSqlSession().insert("RoleSql.uncorrelationMenusByAppId",params);
	}

	@PostConstruct
	public void injectSessionFactory(){
		super.setSqlSessionFactory(sqlSessionFactory);
	}
}
